var group_edit_link = webrootpath+"group/group_edit_link";  //编辑组织
var group_delete_link = webrootpath+"group/group_delete_link";  //删除组织
var group_search_link = webrootpath+"group/group_search_link";  //查询组织列表
var group_user_list_link = webrootpath+"group/group_user_list_link"; //组织和用户关系列表
var add_thisgroup_link = webrootpath+"group/add_thisgroup_link";//加入该组织
var selectalluserlik = webrootpath+"user/selectalluser"; //查询所有用户
var selectuserbygroup_link = webrootpath+"group/selectuserbygroup_link";//查询某个组织下的用户id
var delete_thisgroup_link = webrootpath+"group/delete_thisgroup_link";//删除某个组织下的用户
var search_thisgroupuser_link = webrootpath+"group/search_thisgroupuser_link";//分页查看该组织下的所有用户

var edit_menu_authlink = webrootpath+"group/edit_menu_authlink";//编辑可见菜单权限
var edit_menu_authclk_bindlink = webrootpath+"group/edit_menu_authclk_bindlink";//可见标签绑定组织id

var edit_menu_authgroupid = "0";//选中组织
$(function () {
    selectgrouplist(1);
});

/***
 * 增加/取消标签授权
 */
function edit_menu_authclk(_this) {
    //var name = $(_this).prop("name");
    var name = $(_this).attr("zhname");
    var val = $(_this).val();
    var ischeck = $(_this).prop("checked");
    $.post(edit_menu_authclk_bindlink,{
        groupid:edit_menu_authgroupid,
        name:name,
        val:val,
        ischeck:ischeck
    },function (_info) {
        if(_info=="1"){
            //success
        }
    });


}
/***
 * 编辑可见菜单
 */
function edit_menu_auth(_groupid,_pagenum,_groupname) {
    edit_menu_authgroupid = _groupid;
    $("#index_user_group_menuauth").modal("show");
    $.get(edit_menu_authlink,{
        groupid:_groupid
    },function (_info) {
        if(_info.code=="1"){
            var datalist = _info.data;
            var htmlstr = "";

            for (var i in datalist){
                var datamap = datalist[i];
                var name = datamap.name;
                var ids  = datamap.id;
                var checked = datamap.checked;
                var checkedstr = "";
                if(checked=="true"){
                    checkedstr = "checked='checked'";
                }
                htmlstr+="<tr>" +
                    "<td>"+name+"</td>" +
                    "<td><input zhname='"+name+"' name='index_user_group_menuauth_kname' onclick='edit_menu_authclk(this)' value='"+ids+"' type=\"checkbox\" "+checkedstr+" ></td>" +
                    "</tr>";

            }

            $("#index_user_group_menuauth_datalist").html(htmlstr);
        }
    });
}

/***
 * 全选
 */
function index_user_group_menuauth_allck() {
    $("input[name='index_user_group_menuauth_kname']").click();
}
/***
 * 移出该组织
 *
 */
function delete_thisgroup(_userid) {
    var _groupid = sessionStorage.getItem("group_importuser_groupid");
    $.post(delete_thisgroup_link,{
        groupid:_groupid,
        userid:_userid
    },function (_info) {
        console.log(_info);
        if(_info.code=="1"){
            var group_importuser_groupid = sessionStorage.getItem("group_importuser_groupid");
            var group_importuser_groupname =  sessionStorage.getItem("group_importuser_groupname");
            var group_importuser_pagenum =  sessionStorage.getItem("group_importuser_pagenum");
            group_importuser(group_importuser_groupid,group_importuser_pagenum,group_importuser_groupname);
        }
    });
}
/***
 * 加入该组织
 */
function add_thisgroup(_userid) {
    var _groupid = sessionStorage.getItem("group_importuser_groupid");
    $.post(add_thisgroup_link,{
        groupid:_groupid,
        userid:_userid
    },function (_info) {
        console.log(_info);
        if(_info.code=="1"){
            var group_importuser_groupid = sessionStorage.getItem("group_importuser_groupid");
            var group_importuser_groupname =  sessionStorage.getItem("group_importuser_groupname");
            var group_importuser_pagenum =  sessionStorage.getItem("group_importuser_pagenum");
            group_importuser(group_importuser_groupid,group_importuser_pagenum,group_importuser_groupname);
        }
    });
}

/****
 * 查看组内用户
 * @param _groupid
 * @param pagenum
 * @param _goupname
 */
function search_thisgroupuser(_groupid,_pagenum,_groupname) {
    sessionStorage.setItem("group_thisgroupuser_groupid",_groupid);
    sessionStorage.setItem("group_thisgroupuser_groupname",_groupname)
    $("#group_searchuser_dislog_modal_gname").html(_groupname);
    $("#group_searchuser_dislog").modal('show');
    $.get(search_thisgroupuser_link,{
        groupid:_groupid,
        pagenum:_pagenum
    },function (_info) {
        console.log(_info);
        $("#group_searchuser_dislog_userlist").html("暂无数据");
        if(_info.code=="1"){
            var datalist = _info.data.userlist;
            var pagetotal = _info.data.pagetotal;
            search_thisgroupuser_page(_pagenum,pagetotal);
            var htmlstr = "";
            for(var i in datalist){
                var contentobj = datalist[i];
                htmlstr += "<tr>" +
                    "<td>"+contentobj.userid+"</td>" +
                    "<td>"+contentobj.username+"</td>" +
                    "<td>"+contentobj.usertruename+"</td>" +
                    "<td>"+timestampToTime(contentobj.userdate)+"</td>" +
                    "</tr>";

            }
            $("#group_searchuser_dislog_userlist").html(htmlstr);
        }
    });
}
//数据分页
function search_thisgroupuser_page(_pagenum,_pagezong){
    //处理分页
    $('#group_searchuser_dislog_userlist_page').bootstrapPaginator({
        currentPage: _pagenum,
        totalPages: _pagezong,
        size:"normal",
        bootstrapMajorVersion: 3,
        alignment:"right",
        numberOfPages:3,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "末页";
                case "page":
                    return page;
            }
        },
        onPageClicked: function (event, originalEvent, type, page) {
            var groupid = sessionStorage.getItem("group_thisgroupuser_groupid");
            var groupname =  sessionStorage.getItem("group_thisgroupuser_groupname");
            search_thisgroupuser(groupid,page,groupname);
        }
    });
}
/****
 * 组织内导入用户,弹出框
 * @param _groupid
 */
function group_importuser(_groupid,pagenum,_goupname) {
    sessionStorage.setItem("group_importuser_groupid",_groupid);
    sessionStorage.setItem("group_importuser_groupname",_goupname)
    $("#group_importuser_dislog_modal_gname").html(_goupname);
    $("#group_importuser_dislog").modal('show');
    //查看该组织下的用户
    $.get(selectuserbygroup_link,{
        groupid:_groupid
    },function (_info) {
        var _thisgroupuserid = _info.data;
        $.ajax({
            type:"post",
            url:selectalluserlik,
            data:{
                pagenum:pagenum
            },
            async:true,
            cache:false,
            dataType:"json",
            success:function(content){
                $("#group_importuser_dislog_userlist").html("暂无数据");
                if(content.code=="1"){
                    var datalist = content.data.datalist;
                    var pagetotal = content.data.pagetotal;
                    var pagesize = content.data.pagesize;
                    var pagenum = content.data.pagenum;
                    group_importuser_page(pagenum,pagetotal);
                    var htmlstr = "";
                    for(var i in datalist){
                        var contentobj = datalist[i];

                        var isgroup = false;
                        for(var j in _thisgroupuserid){
                            var tgu = _thisgroupuserid[j];
                            if(contentobj.userid==tgu){
                                isgroup = true;
                            }
                        }
                        if(isgroup==true){
                            htmlstr += "<tr>" +
                                "<td>"+contentobj.userid+"</td>" +
                                "<td>"+contentobj.username+"</td>" +
                                "<td>"+contentobj.usertruename+"</td>" +
                                "<td>"+timestampToTime(contentobj.userdate)+"</td>" +
                                "<td> " +
                                "<button type='button' class='btn btn-danger' onclick='delete_thisgroup(\""+contentobj.userid+"\")'>移出组织</button>" +
                                "</td>" +
                                "</tr>";
                        }else {
                            htmlstr += "<tr>" +
                                "<td>"+contentobj.userid+"</td>" +
                                "<td>"+contentobj.username+"</td>" +
                                "<td>"+contentobj.usertruename+"</td>" +
                                "<td>"+timestampToTime(contentobj.userdate)+"</td>" +
                                "<td> " +
                                "<button type='button' class='btn btn-info'   onclick='add_thisgroup(\""+contentobj.userid+"\")' >加入组织</button>" +
                                "</td>" +
                                "</tr>";
                        }
                    }
                    $("#group_importuser_dislog_userlist").html(htmlstr);
                }
            }
        });

    });




}
//数据分页
function group_importuser_page(_pagenum,_pagezong){
    sessionStorage.setItem("group_importuser_pagenum",_pagenum);
    //处理分页
    $('#group_importuser_dislog_userlist_page').bootstrapPaginator({
        currentPage: _pagenum,
        totalPages: _pagezong,
        size:"normal",
        bootstrapMajorVersion: 3,
        alignment:"right",
        numberOfPages:3,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "末页";
                case "page":
                    return page;
            }
        },
        onPageClicked: function (event, originalEvent, type, page) {
            var group_importuser_groupid = sessionStorage.getItem("group_importuser_groupid");
            var group_importuser_groupname =  sessionStorage.getItem("group_importuser_groupname");
            group_importuser(group_importuser_groupid,page,group_importuser_groupname);
        }
    });
}
/***
 * 查询组织列表
 */
function selectgrouplist(_pagenum) {
    $.post(group_search_link,{
        pagenum:_pagenum
    },function (content) {
        if(content.code=='1'){
            var datalist = content.data.datalist;
            var pagetotal = content.data.pagetotal;
            var pagesize = content.data.pagesize;
            var pagenum = content.data.pagenum;
            selectgrouplistpage(pagenum,pagetotal);
            var htmlstr = "";
            for(var i in datalist){
                var contentobj = datalist[i];

                var groupstatusstr = "其它";
                if(contentobj.groupstatus=='10'){
                    groupstatusstr = "正常";
                }
                htmlstr += "<tr id='group_tr"+contentobj.groupid+"' >" +
                                "<td><input name='' value='"+contentobj.groupid+"' type='checkbox'>"+contentobj.groupid+"</td>" +
                                "<td>"+contentobj.groupname+"</td>" +
                                "<td>"+contentobj.groupdesc+"</td>" +
                                "<td><input style='display: none' value='"+contentobj.groupstatus+"' />  "+groupstatusstr+"</td>" +
                                "<td>"+timestampToTime(contentobj.groupdate)+"</td>" +
                                "<td>" +
                                    "<button type='button' class='btn btn-danger' onclick='group_delete(\""+contentobj.groupid+"\")'>删除组织</button>" +
                                    "<button type='button' class='btn btn-info'   onclick='group_update(\""+contentobj.groupid+"\")' style='margin-left: 20px;'  >修改组织</button>" +
                                    "<button type='button' class='btn btn-info'   onclick='group_importuser(\""+contentobj.groupid+"\",1,\""+contentobj.groupname+"\")' style='margin-left: 20px;'  >编辑组内用户</button>" +
                                    "<button type='button' class='btn btn-info'   onclick='search_thisgroupuser(\""+contentobj.groupid+"\",1,\""+contentobj.groupname+"\")' style='margin-left: 20px;'  >查看组内用户</button>" +
                                    "<button type='button' class='btn btn-info'   onclick='edit_menu_auth(\""+contentobj.groupid+"\",1,\""+contentobj.groupname+"\")' style='margin-left: 20px;'  >编辑可见菜单</button>" +
                                "</td>" +
                            "</tr>";
            }


            $("#index_div_2_2_1_grouplist").html(htmlstr);


        }else {
            $("#index_div_2_2_1_grouplist").html("没有组织数据");
        }
    });
}
//数据分页
function selectgrouplistpage(_pagenum,_pagezong){
    //处理分页
    $('#index_div_2_2_1_grouplist_page').bootstrapPaginator({
        currentPage: _pagenum,
        totalPages: _pagezong,
        size:"normal",
        bootstrapMajorVersion: 3,
        alignment:"right",
        numberOfPages:5,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "末页";
                case "page":
                    return page;
            }
        },
        onPageClicked: function (event, originalEvent, type, page) {
            selectgrouplist(page);
        }
    });
}


/***
 * 增加组织
 */
function group_add() {
    $("#edit_group_dialog_groupname").val("");
    $("#edit_group_dialog_groupdesc").val("");
    $("#edit_group_dialog").modal('show');
    $("#edit_group_dialog_subid").one("click",function () {
        var groupname = $.trim($("#edit_group_dialog_groupname").val());
        var groupdesc = $.trim($("#edit_group_dialog_groupdesc").val());
        var groupstatus = $("#edit_group_dialog_groupstatus").val();
        //增加
        $.post(group_edit_link,{
            groupname:groupname,
            groupdesc:groupdesc,
            groupstatus:groupstatus,
            type:1
        },function (content) {
            $("#edit_group_dialog").modal('hide');
            if(content.code=="1"){
                $.alert({content: '操作成功'});
                selectgrouplist(1);
            }else {
                $.alert({content: '操作失败'});

            }
        });
    });
}

/***
 * 修改组织
 */
function group_update(_groupid) {
    $("#edit_group_dialog").modal('show');
    var grouptrobj = "#group_tr"+_groupid;
    $("#edit_group_dialog_groupname").val($(grouptrobj).find("td:eq(1)").text());
    $("#edit_group_dialog_groupdesc").val($(grouptrobj).find("td:eq(2)").text());
    var tcont = $(grouptrobj).find("td:eq(3)").find("input:eq(0)").val();
    $("#edit_group_dialog_groupstatus").val(tcont);
    $("#edit_group_dialog_subid")
    $("#edit_group_dialog_subid").one("click",function () {
        //修改
        var groupname = $.trim($("#edit_group_dialog_groupname").val());
        var groupdesc = $.trim($("#edit_group_dialog_groupdesc").val());
        var groupstatus = $("#edit_group_dialog_groupstatus").val();
        $.post(group_edit_link,{
            groupname:groupname,
            groupdesc:groupdesc,
            groupstatus:groupstatus,
            type:2,
            groupid:_groupid
        },function (content) {
            $("#edit_group_dialog").modal('hide');
            if(content.code=="1"){
                $.alert({content: '操作成功'});
                selectgrouplist(1);
                return false;
            }else {
                $.alert({content: '操作失败'});
            }
        });
    });
}

/***
 * 删除组织
 */
function group_delete(_groupid) {
    var ts = $.confirm({
        title: '提示',
        content: '您确定删除该组织吗？',
        buttons: {
            '确定': function () {
                ts.close();
                $.get(group_delete_link,{
                    groupid:_groupid
                },function (content) {
                    if(content.code=="1"){
                        $.alert({content: '操作成功'});
                        selectgrouplist(1);
                    }else {
                        $.alert({content: '操作失败'});
                    }
                });
            },
            '取消': function () {
                ts.close();
            }
        }
    });
}
